package activity;

import android.app.Dialog;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jaden.washer.ActivityManagement;
import com.example.jaden.washer.MyApplication;
import com.example.jaden.washer.R;
import com.example.jaden.washer.TransferData;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import customControls.Pattern;
import customControls.WashPatternAdapter;
import customControls.WasherAdapter;
import dormitory.Dormitory;
import order.Order;
import washer.Washer;

public class WasherPage extends AppCompatActivity implements AdapterView.OnItemClickListener {

    TextView dormloc;
    TextView washnumber;
    ListView washerlistview;
    WasherAdapter washerAdapter;
    MyApplication application;
    List<Washer> washerList;
    Pattern pattern;
    List<Pattern>patternList= new ArrayList<>();
    boolean ischoose;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.washerpage);
        //将当前活动加入list中进行管理
        ActivityManagement.activityList.add(this);
        getInstance();
        initwasher();
        washerlistview.setOnItemClickListener(this);
    }
    //获取控件实例
    private void getInstance() {
        dormloc=(TextView)findViewById(R.id.textView_dormloc_washerpage);
        washnumber=(TextView)findViewById(R.id.textView_washnumber_washerpage);
        washerlistview =(ListView)findViewById(R.id.ListView_washer_washerpage);
        application= MyApplication.getInstance();
    }
    //显示洗衣机
    private void initwasher()
    {
        Dormitory dormitory=application.getCurDorm();
        try {
            String url = "http://47.96.159.236:8080/Wash/Washer?method=getWasher";
            JSONObject userJson = new JSONObject();
            userJson.put("DormID", dormitory.getDormID());
            String messageJson = String.valueOf(userJson);
            TransferData.GetWasherList(url, messageJson);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        while (true) {
            if (application.isGetWasherOk()) {
                application.setGetWasherOk(false);
                washerList = (List<Washer>) application.getResponseMessage().getData();
                application.setWasherList(washerList);
                if(washerList==null||washerList.isEmpty())
                    washerAdapter=new WasherAdapter(WasherPage.this,R.layout.washer_listview_item_washingmachine,new ArrayList<Washer>());
                else
                    washerAdapter=new WasherAdapter(WasherPage.this,R.layout.washer_listview_item_washingmachine,washerList);
                washerlistview.setAdapter(washerAdapter);
                break;
            }
        }
        dormloc.setText(dormitory.getDormLocation());
        washnumber.setText(String.valueOf(dormitory.getWasherNumber()));

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Washer washer=washerList.get(position);
        Toast.makeText(this,"使用"+washer.getWasherID(),Toast.LENGTH_LONG).show();
        dialogShow(washer,position);

    }
    private void dialogShow(final Washer washer, int position)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(R.layout.purchase_dialog, null);
        TextView number=(TextView)v.findViewById(R.id.textView_number_purchasedialog);
        TextView washID=(TextView)v.findViewById(R.id.textView_washerID_purchasedialog);
        TextView isfree=(TextView)v.findViewById(R.id.textView_isfree_purchasedialog);
        ListView washpattern=(ListView)v.findViewById(R.id.ListView_washpattern_purchasedialog);
        Button back=(Button)v.findViewById(R.id.button_back_purchasedialog);
        Button book=(Button)v.findViewById(R.id.button_book_purchasedialog);
        number.setText(number.getText()+ String.valueOf(position+1));
        washID.setText("洗衣机ID:"+washer.getWasherID());
        if(washer.isIsFree()) {
            isfree.setText("空闲");
            isfree.setTextColor(getResources().getColor(R.color.colorPrimary));
            isfree.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.washmaching),null,null,null);
        }
        else
        {
            isfree.setText("使用中");
            isfree.setTextColor(getResources().getColor(R.color.colorred));
            isfree.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.washerusing),null,null,null);
        }

        initWashingType();
        washpattern.setSelector(R.color.darkgrey);
        WashPatternAdapter washPatternAdapter=new WashPatternAdapter(this,R.layout.washpattern_listview_item,patternList);
        washpattern.setAdapter(washPatternAdapter);
        final Dialog dialog=builder.create();
        dialog.show();
        dialog.getWindow().setContentView(v);
        dialog.getWindow().setGravity(Gravity.BOTTOM);
        ischoose=false;
        washpattern.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ischoose=true;
                pattern=patternList.get(position);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        book.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ischoose)
                {
                    /**
                     * 进行预定
                     */

                    try {
                        String url="http://47.96.159.236:8080/Wash/Order?method=submit";
                        JSONObject userJson=new JSONObject();
                        userJson.put("DormID",application.getCurDorm().getDormID());
                        userJson.put("StuID",application.getStudent().getStuID());
                        userJson.put("WasherID",washer.getWasherID());
                        userJson.put("OrderPattern",pattern.getPattern());
                        userJson.put("OrderMoney", pattern.getPrice());
                        String messageJson=String.valueOf(userJson);
                        TransferData.CommitOrder(url,messageJson);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    while(true) {
                        if(application.isCommitorderOK()) {
                            application.setCommitorderOK(false);
                            //Order order= (Order) application.getResponseMessage().getData();
                            //application.
                            initwasher();
                            dialog.dismiss();
                            break;
                        }
                    }
                }
                else {
                    Toast.makeText(WasherPage.this,"请选择洗衣类型",Toast.LENGTH_LONG).show();
                }
            }
        });
    }
    private void initWashingType()
    {

        Pattern pattern=new Pattern();
        pattern.setInterval(String.valueOf(40));
        pattern.setPrice(4);
        pattern.setPattern(2);
        pattern.setType("快洗");
        patternList.add(pattern);
        Pattern pattern1=new Pattern();
        pattern1.setInterval(String.valueOf(45));
        pattern1.setPrice(5);
        pattern1.setType("标准");
        pattern1.setPattern(1);
        patternList.add(pattern1);
    }
}
